package com.siruier.boss.ui.activity.mall

import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.*
import com.siruier.boss.R
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.mallApi
import com.siruier.boss.bean.MallCategoryBean
import com.siruier.boss.bean.MallCategorySimpleBean
import com.siruier.boss.common.ADDividerItemDecoration
import com.siruier.boss.databinding.ActivityGoodsCategoryBinding
import com.siruier.boss.databinding.ItemGoodsCategoryOneBinding
import com.siruier.boss.databinding.ItemGoodsCategoryTwoBinding
import com.siruier.boss.databinding.ItemGoodsCategoryTwoDetailBinding
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.base.BaseSignCheckAdapter
import com.siruier.boss.ui.ext.dp
import com.siruier.boss.ui.ext.startActivityExt
import com.siruier.boss.ui.ext.toColor
import com.siruier.boss.ui.utils.FullyGridLayoutManager
import com.siruier.boss.ui.utils.GlideUtils
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.ArrayList

typealias OnClickCategory = (onePosition: Int, twoPosition: Int) -> Unit

class GoodsCategoryActivity : BaseActivity<ActivityGoodsCategoryBinding>() {

    private val mGoodsCategoryOneAdapter = GoodsCategoryOneAdapter()
    private lateinit var mGoodsCategoryTwoAdapter: GoodsCategoryTwoAdapter
    private lateinit var mManagerRight: LinearLayoutManager
    private var mIsFromClick: Boolean = false
    private lateinit var topScroller: LinearSmoothScroller

    override fun initView() {
        bindViewClick(vb.searchBar, vb.ivBack)

        vb.rvOne.layoutManager = LinearLayoutManager(mThis)
        vb.rvOne.addItemDecoration(ADDividerItemDecoration(mThis))
        (vb.rvOne.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
        vb.rvOne.adapter = mGoodsCategoryOneAdapter

        topScroller = object : LinearSmoothScroller(this) {
            override fun getHorizontalSnapPreference() = SNAP_TO_START
            override fun getVerticalSnapPreference() = SNAP_TO_START
            override fun onStop() {
                super.onStop()
                lifecycleScope.launch {
                    delay(500)
                    mIsFromClick = false
                }
            }
        }

        mGoodsCategoryOneAdapter.setOnClickAdapterListenter { view, position ->
            mIsFromClick = true
            topScroller.targetPosition = position
            mManagerRight.startSmoothScroll(topScroller)
        }

        mGoodsCategoryTwoAdapter = GoodsCategoryTwoAdapter { onePosition, twoPosition ->
            val category = mGoodsCategoryOneAdapter.datas[onePosition]
            startActivityExt<CategoryDetailActivity> {
                putExtra("categoryId", category.children[twoPosition].id)
            }
        }
        mManagerRight = LinearLayoutManager(mThis)
        vb.rvTwo.layoutManager = mManagerRight
        vb.rvTwo.adapter = mGoodsCategoryTwoAdapter

        launchUI({
            val categoryAll = mallApi.categoryAll().toData() ?: mutableListOf()
            if (categoryAll.isNotEmpty()) {
                categoryAll[0].check = true
            }
            mGoodsCategoryOneAdapter.datas = categoryAll
            mGoodsCategoryOneAdapter.notifyDataSetChanged()

            mGoodsCategoryTwoAdapter.datas = categoryAll
            mGoodsCategoryTwoAdapter.notifyDataSetChanged()

        })

        vb.rvTwo.addOnScrollListener(object : RecyclerView.OnScrollListener() {
//            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
//                super.onScrolled(recyclerView, dx, dy)
//                if (mIsFromClick) {
//                    return
//                }
//                changePosition()
//            }

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (!mIsFromClick) {
                        changePosition()
                    }
                }
            }
        })
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.searchBar -> startActivityExt<GoodsSearchActivity>()
            vb.ivBack -> finish()
        }
    }

    private fun changePosition() {
        val firstPosition = mManagerRight.findFirstVisibleItemPosition()
        if (mGoodsCategoryOneAdapter.getCheckPosition() != firstPosition) {
            mGoodsCategoryOneAdapter.checkPosition(firstPosition)
        }
    }

    class GoodsCategoryOneAdapter :
        BaseSignCheckAdapter<ItemGoodsCategoryOneBinding, MallCategoryBean>() {
        override fun onBindViewHolder(
            vb: ItemGoodsCategoryOneBinding,
            bean: MallCategoryBean,
            position: Int
        ) {
            vb.tvText.text = bean.name
            if (bean.check) {
                vb.tvText.setTextColor(R.color.mall_main.toColor())
                vb.tvText.setBackgroundColor(R.color.white.toColor())
            } else {
                vb.tvText.setTextColor(R.color.text_color.toColor())
                vb.tvText.setBackgroundColor(R.color.main_bg.toColor())
            }
            setViewClick(position, vb.root)
        }
    }

    class GoodsCategoryTwoAdapter(
        private val onClickCategory: OnClickCategory
    ) : BaseSignCheckAdapter<ItemGoodsCategoryTwoBinding, MallCategoryBean>() {
        override fun onBindViewHolder(
            vb: ItemGoodsCategoryTwoBinding,
            bean: MallCategoryBean,
            position: Int
        ) {
            if (position == 0) {
                vb.root.setPadding(0, 5.dp, 0, 0)
            } else {
                vb.root.setPadding(0, 20.dp, 0, 0)
            }
            vb.tvText.text = bean.name
            if (vb.rvCategory.layoutManager == null) {
                vb.rvCategory.layoutManager = FullyGridLayoutManager(vb.rvCategory.context, 3)
            }
            val adapter = (vb.rvCategory.adapter as? GoodsCategoryTwoDetailAdapter)
                ?: GoodsCategoryTwoDetailAdapter(onClickCategory).also {
                    vb.rvCategory.adapter = it
                }
            adapter.onePosition = position
            adapter.datas = bean.children
            adapter.notifyDataSetChanged()
            setViewClick(position, vb.root)
        }
    }

    class GoodsCategoryTwoDetailAdapter(
        private val onClickCategory: OnClickCategory
    ) : BaseSignCheckAdapter<ItemGoodsCategoryTwoDetailBinding, MallCategoryBean>() {

        var onePosition: Int = 0

        override fun onBindViewHolder(
            vb: ItemGoodsCategoryTwoDetailBinding,
            bean: MallCategoryBean,
            position: Int
        ) {
            vb.tvText.text = bean.name
            GlideUtils.intoView(vb.ivImage.context, bean.picUrl, vb.ivImage)
            setViewClick(position, vb.root)
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            onClickCategory.invoke(onePosition, position)
        }
    }
}